#!/snail/common/utils groovy DLGlassAL_20220329

import groovy.json.JsonSlurper;
import groovy.json.JsonOutput;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.form.service.FormService;

import com.seeyon.apps.snail.APPLogger;
import com.seeyon.apps.snail.common.MessageHepler;

import snail.common.utils.StringHelper;
import snail.common.utils.cipher.RSACoder;
import snail.common.utils.cipher.SHA256Coder;
import snail.common.utils.cipher.MD5Coder;
import snail.common.utils.stream.StreamUtils;
import snail.common.utils.CacheHelper;
import snail.common.utils.LocalCacheHelper;
import snail.common.utils.MapHelper;
import snail.common.utils.DateHelper;
import snail.common.utils.IDHelper;
import snail.common.utils.JSONHelper;
import snail.common.web.HTTPHelper;
import snail.common.ServiceException;


/**
############################ 【Groovy触发器】配置说明 ############################
# 
# 测试单据数据(流程事件)
# 		
################################################################################ */
static void main(String[] args){
	
	/* ################################### 初始化 ################################### */		
	HTTPHelper.isDebug();
	HTTPHelper.setLogFile(new File(AppContext.getCfgHome(), ('../../../../logs/snail.httphelper.DLGlassAL.' + DateHelper.getCurrentDate("yyyy-MM-dd") + '.log')));
	/* ########################################################################### */
	
	def billno = 'UNCODE';
	def billsData = null;
	def msgReceiver = null;
	
	try {
		
		// 获取单据编码
		billno = getBusinessAttributeValue('trigger_billno', billno);
		// 获取单据数据
		billsData = getBusinessBillsData();
		if (!__IS_TRIGGER__) {
			switch(__TRIGGER_EVENT_TYPE__) {
					
				case '__ON_BEFORE_FINISHWORKITEM__':
					
					sendTriggerMessage('被处理前事件调用...', msgReceiver);
					
					if(StringHelper.isBlank(billno)){
						return  '【billno】为空！！！！';
					}
					def res = sendToU9(billno, billsData);
					return res;
					break;
					
			}
		}
		
		// 测试：发送信息
		sendTriggerMessage('测试（CODE: ' + billno + '），单据数据: ');
			
	} catch(ServiceException e){
		
		sendTriggerMessage('测试单据数据失败！！（CODE: ' + billno + '），ERROR: ' + e.message, msgReceiver);
		
	} catch(Throwable e){
		
		APPLogger.error(('测试单据数据失败！！！（CODE: ' + billno + '），ERROR: ' + e.message), e);
		
		sendTriggerMessage('测试单据数据失败！！！！（CODE: ' + billno + '），ERROR: ' + StringHelper.toString(e), msgReceiver);
	}
}
/**
获取sf系统的token
*/
def sendToU9(billno, billsData) throws Exception {
	try{       
		def url = getBusinessAttributeValue('host');
		def EntCode = getBusinessAttributeValue('EntCode');
		def OrgCode = getBusinessAttributeValue('OrgCode');
		def UserCode = getBusinessAttributeValue('UserCode');

	    def header = [
    		'Content-Type': 'application/json;charset=utf-8',
    		'Accept': 'application/json'
    	];

        def body = [
		    context: [
			    EntCode: EntCode,
			    OrgCode: OrgCode,
			    UserCode: UserCode
		    ],
		    jsonData: JSONHelper.stringify(billsData)
	    ];

        // System.out.println(JSONHelper.stringify(body));

        def result = HTTPHelper.post(url, body, header);
        result = JSONHelper.parse(result);
	    result = JSONHelper.parse(result.d);

        if(result.IsSuccess){
            setValue('U9号', result.DocNo);
            sendTriggerMessage("生成U9玻璃铝数据成功" + result.Msg + "单据号 CODE: ${billno}");
        }else{
            return result.Msg;
        }
        return null;
		
	}catch(Throwable e){
		throw new ServiceException("生成u9玻璃铝数据失败！，ERROR: ${e.message}", e);
	}
	
}


/** ******************************* 以下为共公函数 ******************************* */

include seeyon.common.groovy;
include snail.common.trigger2.groovy;




